package com.fingard.dsp.bank.directbank.icbc41;

import com.fingard.dsp.bank.directbank.bankInterface.ICancelAuthorizeForRec;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.*;
import com.fingard.dsp.bank.reqrespobj.authcancelforrec.detail.RetAtsQryRecAuthCancelInfoDetail;
import com.fingard.dsp.bank.reqrespobj.authforrec.RetAtsAuthForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.RetAtsQryAuthForRec;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ICBC41AuthorizeCancelForRec extends ICBC41Base implements ICancelAuthorizeForRec {
    @Override
    public RetAtsAuthCancelForRec d_AuthCancel(ReqAuthCancelForRec p_req) throws Exception {
        RetAtsAuthCancelForRec tmpRetAts = new RetAtsAuthCancelForRec(p_req.reqHeader);
        try {
            throw new Exception("银行目前暂不支持线上签约！");
        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        tmpRetAts.writeRetToATS();
        return tmpRetAts;
    }

    @Override
    public RetAtsQryAuthCancelForRec d_qry_AuthCancel(ReqQryAuthCancelForRec p_req) throws Exception {
        RetAtsQryAuthCancelForRec tmpRetAts = new RetAtsQryAuthCancelForRec(p_req.reqHeader);
        tmpRetAts.respCode = "-1";
        tmpRetAts.respInfo = "暂不支持签约状态查询，请通过签约信息查询核对!";
        if (tmpRetAts.getWriteRdCount() <= 0) {
            tmpRetAts.writeRetHead();
        }
        return tmpRetAts;
    }

    @Override
    public RetAtsQryRecAuthCancelInfo d_qry_AuthCancelInfo(ReqQryRecAuthCancelInfo p_req) throws Exception {
        if ("1".equals(p_req.isPrivate)) {
            return d_qry_authInfo_pri(p_req);
        } else {
            return d_qry_authInfo_pri(p_req);
        }
    }

    public RetAtsQryRecAuthCancelInfo d_qry_authInfo_pri(ReqQryRecAuthCancelInfo p_req) throws Exception {
        RetAtsQryRecAuthCancelInfo tmpRetAts = new RetAtsQryRecAuthCancelInfo(p_req.reqHeader);
        tmpRetAts.ownAct = p_req.ownAct;
        try {
            ICBC41Header bankHeader = new ICBC41Header();
            bankHeader.tx_code = "80000";
            bankHeader.seq = p_req.reqHeader.transSeqID;
            bankHeader.sysid = bankFront.org;

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(bankHeader, xmlWriter);
            xmlWriter.writeElementString("ACC_NO", p_req.oppAct);//卡号
            md5Map.put("ACC_NO", p_req.oppAct);
            xmlWriter.writeElementString("ACC_NAME", p_req.oppName);//户名
            md5Map.put("ACC_NAME", p_req.oppName);
            xmlWriter.writeElementString("CERT_TYPE", "01");//证件类型
            md5Map.put("CERT_TYPE", "01");
            xmlWriter.writeElementString("CERT_NO", p_req.oppCertNum);//证件号码
            md5Map.put("CERT_NO", p_req.oppCertNum);
            xmlWriter.writeElementString("MOBILE", p_req.oppCellNum);//手机号
            md5Map.put("MOBILE", p_req.oppCellNum);
            xmlWriter.writeElementString("ADDRESS", "");//地址
            xmlWriter.writeElementString("BUSICODE", p_req.reqReserve1);//客户编号
            md5Map.put("BUSICODE", p_req.reqReserve1);
            xmlWriter.writeElementString("SIGNFLAG", "3");//签约状态（0-签约，1-修改，2-解约，3-查询）
            md5Map.put("SIGNFLAG", "3");
            xmlWriter.writeElementString("RSV", "");//备注
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    tmpRetAts.respInfo = tmpBkReader.getFirstText("/TXN/HEAD/RSP_DESC");
                    if ("0".equals(tmpBkReader.getFirstText("/TXN/HEAD/RSP_CD"))) {
                        tmpRetAts.respCode = "0";
                        RetAtsQryRecAuthCancelInfoDetail tmpDetail = new RetAtsQryRecAuthCancelInfoDetail();
                        tmpDetail.oppAct = p_req.oppAct;
                        tmpDetail.queryTag = p_req.queryTag;
                        tmpRetAts.writeRetDetail(tmpDetail);
                    } else if ("1".equals(tmpBkReader.getFirstText("/TXN/HEAD/RSP_CD"))) {
                        tmpRetAts.respCode = "0";
                    } else {
                        tmpRetAts.respCode = "-1";
                    }
                } else {
                    tmpRetAts.respCode = "-1";
                    tmpRetAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            ex.printStackTrace();
        }
        tmpRetAts.writeAfterDetail();
        return tmpRetAts;
    }

    public RetAtsQryRecAuthCancelInfo d_qry_authInfo_pub(ReqQryRecAuthCancelInfo p_req) throws Exception {
        return null;
    }
}
